﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MySqlConnector;
using MyWpfFramework.Helper;
using MyWpfFramework.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MyWpfFramework.ViewModels
{
    public partial class LoginViewModel:ObservableObject
    {
      
        [ObservableProperty]
        private string?  userName;
        [ObservableProperty]
        private string passWord;
        [RelayCommand]
        private void LoginClick()
        {
            GetCatch();
        }
       
        public void GetCatch()
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                MessageBox.Show("用户名密码不能为空");
                return;
            }
            User? users= SqlsugarHelper.Db.Queryable<User>().Where(x => x.UserName==userName)?.ToList().FirstOrDefault();
            if (users is null)
            {
                MessageBox.Show("用户名密码不能为空");
                return;
            }
            else if (users.Password == passWord)
            {
                WindowService.CloseWindow();
                WindowService.OpenWindow();
            }
            else
            {
                MessageBox.Show("密码错误");
            }

        }  
    }
}
